﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using DnPakExplorer.Forms;


namespace DnPakExplorer.Controls {

	public partial class SkillPanelControl : UserControl {

		private Character mCharacter;

		[Browsable(true)]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
		public override string Text {
			get {
				return base.Text;
			}
			set {
				base.Text = value;
			}
		}

		public Character Character {
			get { return mCharacter; }
			set {
				if (mCharacter != null) {
					mCharacter.SkillNanel.OnSkillItemChanged -= SkillNanel_OnSkillItemChanged;
					mCharacter.OnCharacterClosed -= mCharacter_OnCharacterClosed;
				}
				mCharacter = value;
				if (mCharacter != null) {
					mCharacter.SkillNanel.OnSkillItemChanged += SkillNanel_OnSkillItemChanged;
					mCharacter.OnCharacterClosed += mCharacter_OnCharacterClosed;
					SkillNanel_OnSkillItemChanged();
				}
			}
		}
		

		public SkillPanelControl() {
			InitializeComponent();
			listGridView1.OnGridHover = OnItemHover;
			Disposed += SkillPanelControl_Disposed;
		}

		void mCharacter_OnCharacterClosed(object sender, EventArgs e) {
			Character = null;
			listGridView1.Clear();
			Form form = FindForm();
			if (form is PanelForm) {
				form.Close();
			}
		}

		void SkillPanelControl_Disposed(object sender, EventArgs e) {
			Character = null;
			listGridView1.Clear();
			Form form = FindForm();
			if (form is PanelForm) {
				form.Close();
			}
		}


		void SkillNanel_OnSkillItemChanged() {
			listGridView1.NotifyDataChanged();
			listGridView1.Refresh();
		}

		public void OnItemHover(int row, int column) {
			SkillPanel panel = mCharacter.SkillNanel;
			if (row >=0 && column==0 && panel[row] != null) {
				Skill skill = panel[row];
				listGridView1.Capture = true;
				InfoOverlayForm.ShowItem(skill);
			} else {
				InfoOverlayForm.HideItem();
				listGridView1.Capture = false;
			}
		}

		class SkillListAdapter : ListGridAdapter {
			private SkillPanel mPanel;

			public SkillListAdapter(SkillPanel panel) {
				mPanel = panel;
			}

			public override int GetColumnWidth(int column) {
				switch (column) {
				case 0:
					return 150;
				case 1:
					return 70;
				case 2:
					return 400;
				default:
					return base.GetColumnWidth(column);
				}
			}

			public override void OnDrawGridBackground(ListGridView view, Graphics g, Color color, int row, int column, Rectangle rect) {
				base.OnDrawGridBackground(view, g, color, row, column, rect);
				if (column == 0) {
					int iconSize = Math.Min(Math.Max(rect.Height - 6, 30), 50);
					Rectangle iconRect = new Rectangle(
						rect.X + 3,
						rect.Y + (rect.Height-iconSize)/2,
						iconSize,
						iconSize
					);
					Util.DrawSkillImage(mPanel[row].IconId, iconRect, g);
				}
			}

			public override void OnDrawGrid(ListGridView view, Graphics g, string text, Color color, int row, int column, Rectangle rect) {
				if (column == 0) {
					int iconSize = Math.Min(Math.Max(rect.Height - 6, 30), 50);
					rect.Width = rect.Width - iconSize - 6;
					rect.X += iconSize + 3;
				}
				base.OnDrawGrid(view, g, text, color, row, column, rect);
			}

			public override int RowCount {
				get { return mPanel.SkillCount; }
			}

			public override int ColumnCount {
				get { return 3; }
			}

			public override void GetTopLeft(Grid grid) {
				grid.text = "";
			}

			public override void GetGridData(int row, int column, Grid grid) {
				grid.text = "";
				Skill skill = mPanel[row];
				switch (column) {
				case 0:
					grid.text = skill.Name;
					break;
				case 1:
					grid.text = skill.Level.ToString();
					break;
				case 2:
					grid.text = skill.SkillDescrition;
					break;
				}
				
			}

			public override void GetRowHeader(int row, Grid grid) {
				grid.text = row.ToString();
			}

			public override void GetColumnHeader(int column, Grid grid) {
				grid.text = "";
				switch (column) {
				case 0:
					grid.text = "技能名";
					break;
				case 1:
					grid.text = "等级";
					break;
				case 2:
					grid.text = "描述";
					break;
				}
				
			}
		}
	}


}
